<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/19
 * Time: 15:45
 */

namespace app\admin\controller;


use app\common\model\Dictionary as DictionaryModel;
use app\common\model\SysConfig;
use think\Exception;

class Config extends Admin
{

    /**
     * 配置信息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(){
        $page = input('page',1);
        $limit = input('limit',20);
        $search_word = input('search_word', '', 'trim');
        $name = input('name', '', 'trim');
        $basic = input('basic', '');
        $param = [];
        if($name){ //基础配置
            $param['name'] = $name;
        }
        if($basic !== ''){
            $param['basic'] = $basic;
        }
        if($search_word){
            $param['common'] = ['like','%'.$search_word.'%'];
        }
        $model = new SysConfig();
        $count = $model->where($param)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $list = $model->where($param)->page($page,$limit)->order('id asc')->select();
        return json_tab_msg($count, $page, $list);
    }

    public function getDetail(){
        $name = input('name', '', 'trim');
        if($name){ //基础配置
            $param['name'] = $name;
        }
        $model = new SysConfig();
        $info = $model->where($param)->find();
        json_return_code($info);
    }

    /**
     * 新增修改
     * @return \think\response\Json
     */
    public function modify(){
        $name = input('name','','trim');
        $val = input('val','','trim');

        $model = new SysConfig();
        if($model->isUpdate(true)->save(['val'=>$val],['name'=>$name]) !== false){
            json_return_code('','设置成功');
        }else{
            json_return_code('','设置失败，请重试',201);
        }
    }
}